<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Agent服务选择机制</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: #333;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }
        
        .container {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        h1 {
            color: #2c3e50;
            text-align: center;
            margin-bottom: 30px;
            font-size: 2.2em;
        }
        
        h2 {
            color: #34495e;
            border-bottom: 3px solid #3498db;
            padding-bottom: 10px;
            margin-top: 40px;
        }
        
        .process-flow {
            display: flex;
            flex-direction: column;
            gap: 20px;
            margin: 30px 0;
        }
        
        .step {
            background: linear-gradient(45deg, #f8f9fa, #e9ecef);
            border: 2px solid #3498db;
            border-radius: 10px;
            padding: 20px;
            position: relative;
            transition: all 0.3s ease;
        }
        
        .step:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(52, 152, 219, 0.2);
        }
        
        .step-number {
            position: absolute;
            top: -10px;
            left: 20px;
            background: #3498db;
            color: white;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
        }
        
        .step-title {
            font-size: 1.2em;
            font-weight: bold;
            color: #2c3e50;
            margin-bottom: 10px;
            margin-left: 20px;
        }
        
        .step-content {
            margin-left: 20px;
        }
        
        .flow-diagram {
            background: #f8f9fa;
            border: 2px solid #dee2e6;
            border-radius: 10px;
            padding: 20px;
            margin: 20px 0;
            text-align: center;
        }
        
        .flow-box {
            display: inline-block;
            background: #3498db;
            color: white;
            padding: 15px 25px;
            border-radius: 8px;
            margin: 10px;
            font-weight: bold;
            position: relative;
            min-width: 150px;
        }
        
        .flow-box.input {
            background: #e74c3c;
        }
        
        .flow-box.process {
            background: #f39c12;
        }
        
        .flow-box.output {
            background: #27ae60;
        }
        
        .arrow {
            font-size: 1.5em;
            color: #34495e;
            margin: 0 10px;
        }
        
        .mechanism-card {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 10px;
            padding: 25px;
            margin: 20px 0;
        }
        
        .mechanism-title {
            font-size: 1.3em;
            font-weight: bold;
            margin-bottom: 15px;
            text-align: center;
        }
        
        .mechanism-list {
            list-style: none;
            padding: 0;
        }
        
        .mechanism-list li {
            background: rgba(255,255,255,0.1);
            margin: 10px 0;
            padding: 15px;
            border-radius: 5px;
            border-left: 4px solid #fff;
        }
        
        .example-box {
            background: #ecf0f1;
            border-left: 5px solid #3498db;
            padding: 20px;
            margin: 15px 0;
            border-radius: 0 8px 8px 0;
        }
        
        .code-block {
            background: #2c3e50;
            color: #ecf0f1;
            padding: 20px;
            border-radius: 8px;
            font-family: 'Courier New', monospace;
            overflow-x: auto;
            margin: 15px 0;
        }
        
        .highlight {
            background: #fff3cd;
            padding: 2px 6px;
            border-radius: 3px;
            font-weight: bold;
        }
        
        .comparison-table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        
        .comparison-table th,
        .comparison-table td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        
        .comparison-table th {
            background: #3498db;
            color: white;
            font-weight: bold;
        }
        
        .comparison-table tr:nth-child(even) {
            background: #f8f9fa;
        }
        
        .interactive-demo {
            background: #f8f9fa;
            border: 2px solid #3498db;
            border-radius: 10px;
            padding: 20px;
            margin: 20px 0;
            text-align: center;
        }
        
        .demo-input {
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            width: 300px;
            margin: 10px;
        }
        
        .demo-button {
            background: #3498db;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 1em;
            transition: background 0.3s;
        }
        
        .demo-button:hover {
            background: #2980b9;
        }
        
        .demo-result {
            background: #d4edda;
            border: 1px solid #c3e6cb;
            border-radius: 5px;
            padding: 15px;
            margin: 15px 0;
            min-height: 50px;
        }
        
        @media (max-width: 768px) {
            .flow-box {
                display: block;
                margin: 10px 0;
            }
            
            .demo-input {
                width: 100%;
                box-sizing: border-box;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🤖 AI Agent如何选择调用哪个接口或MCP服务</h1>
        
        <h2>🎯 核心决策流程</h2>
        <div class="flow-diagram">
            <div class="flow-box input">用户输入</div>
            <span class="arrow">→</span>
            <div class="flow-box process">意图识别</div>
            <span class="arrow">→</span>
            <div class="flow-box process">服务匹配</div>
            <span class="arrow">→</span>
            <div class="flow-box output">服务调用</div>
        </div>
        
        <div class="process-flow">
            <div class="step">
                <div class="step-number">1</div>
                <div class="step-title">输入解析与预处理</div>
                <div class="step-content">
                    <p>AI Agent首先对用户输入进行<span class="highlight">自然语言处理</span>，包括：</p>
                    <ul>
                        <li>分词、词性标注、命名实体识别</li>
                        <li>提取关键词和短语</li>
                        <li>理解上下文和语义</li>
                    </ul>
                </div>
            </div>
            
            <div class="step">
                <div class="step-number">2</div>
                <div class="step-title">意图识别与分类</div>
                <div class="step-content">
                    <p>通过<span class="highlight">意图分类模型</span>识别用户的真实需求：</p>
                    <ul>
                        <li>查询类：需要获取信息</li>
                        <li>操作类：需要执行动作</li>
                        <li>分析类：需要数据处理</li>
                    </ul>
                </div>
            </div>
            
            <div class="step">
                <div class="step-number">3</div>
                <div class="step-title">服务发现与匹配</div>
                <div class="step-content">
                    <p>基于识别的意图，在<span class="highlight">服务注册表</span>中查找合适的服务：</p>
                    <ul>
                        <li>根据功能描述匹配</li>
                        <li>检查服务可用性</li>
                        <li>评估服务质量</li>
                    </ul>
                </div>
            </div>
            
            <div class="step">
                <div class="step-number">4</div>
                <div class="step-title">参数构建与调用</div>
                <div class="step-content">
                    <p>构建服务调用所需的参数并执行调用：</p>
                    <ul>
                        <li>从用户输入中提取参数</li>
                        <li>进行参数格式转换</li>
                        <li>执行服务调用</li>
                    </ul>
                </div>
            </div>
        </div>
        
        <h2>🔍 主要决策机制</h2>
        
        <div class="mechanism-card">
            <div class="mechanism-title">1. 基于规则的匹配</div>
            <ul class="mechanism-list">
                <li><strong>关键词匹配</strong>：通过预定义的关键词库进行匹配</li>
                <li><strong>正则表达式</strong>：识别特定的模式和格式</li>
                <li><strong>语义模板</strong>：使用预定义的语义模板进行匹配</li>
            </ul>
        </div>
        
        <div class="mechanism-card">
            <div class="mechanism-title">2. 基于机器学习的分类</div>
            <ul class="mechanism-list">
                <li><strong>意图分类器</strong>：使用训练好的模型识别用户意图</li>
                <li><strong>实体抽取</strong>：提取关键实体信息</li>
                <li><strong>相似度计算</strong>：计算输入与服务描述的相似度</li>
            </ul>
        </div>
        
        <div class="mechanism-card">
            <div class="mechanism-title">3. 基于上下文的推理</div>
            <ul class="mechanism-list">
                <li><strong>对话历史</strong>：考虑之前的对话上下文</li>
                <li><strong>用户偏好</strong>：根据用户历史行为推断偏好</li>
                <li><strong>业务逻辑</strong>：结合业务规则进行推理</li>
            </ul>
        </div>
        
        <h2>📊 传统API vs MCP服务选择对比</h2>
        
        <table class="comparison-table">
            <tr>
                <th>对比维度</th>
                <th>传统API调用</th>
                <th>MCP服务调用</th>
            </tr>
            <tr>
                <td>服务发现</td>
                <td>硬编码的服务列表</td>
                <td>动态的服务注册表</td>
            </tr>
            <tr>
                <td>匹配方式</td>
                <td>基于预定义规则</td>
                <td>基于标准化的服务描述</td>
            </tr>
            <tr>
                <td>参数处理</td>
                <td>针对每个API单独处理</td>
                <td>统一的参数格式</td>
            </tr>
            <tr>
                <td>扩展性</td>
                <td>添加新服务需要修改代码</td>
                <td>新服务自动注册和发现</td>
            </tr>
            <tr>
                <td>上下文处理</td>
                <td>需要自己实现</td>
                <td>协议层面支持</td>
            </tr>
        </table>
        
        <h2>💡 实际示例演示</h2>
        
        <div class="example-box">
            <h3>示例1：天气查询</h3>
            <p><strong>用户输入：</strong>"明天北京的天气怎么样？"</p>
            <div class="code-block">
1. 意图识别：查询类 - 天气信息
2. 实体抽取：时间="明天", 地点="北京"
3. 服务匹配：WeatherService
4. 参数构建：{location: "北京", date: "2025-07-10"}
5. 调用执行：weather_api.get_forecast(location, date)
            </div>
        </div>
        
        <div class="example-box">
            <h3>示例2：数据分析</h3>
            <p><strong>用户输入：</strong>"分析一下上个月的销售数据"</p>
            <div class="code-block">
1. 意图识别：分析类 - 数据分析
2. 实体抽取：时间="上个月", 数据类型="销售数据"
3. 服务匹配：DataAnalysisService
4. 参数构建：{data_type: "sales", period: "2025-06"}
5. 调用执行：analytics_api.analyze_sales_data(period)
            </div>
        </div>
        
        <h2>🔧 交互式演示</h2>
        
        <div class="interactive-demo">
            <h3>体验AI Agent的服务选择过程</h3>
            <p>输入您的问题，看看AI Agent会如何选择服务：</p>
            <input type="text" class="demo-input" id="userInput" placeholder="例如：查询今天的股票价格">
            <br>
            <button class="demo-button" onclick="processInput()">分析服务选择</button>
            <div class="demo-result" id="result"></div>
        </div>
        
        <h2>🚀 优化策略</h2>
        
        <div class="mechanism-card">
            <div class="mechanism-title">提升服务选择准确性的方法</div>
            <ul class="mechanism-list">
                <li><strong>多模型融合</strong>：结合规则、机器学习、深度学习等方法</li>
                <li><strong>动态学习</strong>：根据用户反馈不断优化匹配算法</li>
                <li><strong>A/B测试</strong>：对不同的匹配策略进行测试</li>
                <li><strong>服务质量监控</strong>：实时监控服务性能，动态调整优先级</li>
            </ul>
        </div>
    </div>
    
    <script>
        function processInput() {
            const input = document.getElementById('userInput').value;
            const result = document.getElementById('result');
            
            if (!input.trim()) {
                result.innerHTML = '<p style="color: #e74c3c;">请输入您的问题</p>';
                return;
            }
            
            // 模拟AI Agent的决策过程
            let analysis = '';
            
            // 简单的关键词匹配示例
            if (input.includes('天气') || input.includes('温度') || input.includes('下雨')) {
                analysis = `
                    <h4>🌤️ 服务选择分析：</h4>
                    <p><strong>1. 意图识别：</strong>查询类 - 天气信息</p>
                    <p><strong>2. 关键词提取：</strong>天气相关词汇</p>
                    <p><strong>3. 匹配服务：</strong>WeatherService</p>
                    <p><strong>4. 调用方式：</strong>weather_api.getCurrentWeather()</p>
                `;
            } else if (input.includes('股票') || input.includes('价格') || input.includes('投资')) {
                analysis = `
                    <h4>📈 服务选择分析：</h4>
                    <p><strong>1. 意图识别：</strong>查询类 - 金融信息</p>
                    <p><strong>2. 关键词提取：</strong>股票、价格等金融词汇</p>
                    <p><strong>3. 匹配服务：</strong>FinanceService</p>
                    <p><strong>4. 调用方式：</strong>finance_api.getStockPrice()</p>
                `;
            } else if (input.includes('分析') || input.includes('数据') || input.includes('报告')) {
                analysis = `
                    <h4>📊 服务选择分析：</h4>
                    <p><strong>1. 意图识别：</strong>分析类 - 数据处理</p>
                    <p><strong>2. 关键词提取：</strong>分析、数据等关键词</p>
                    <p><strong>3. 匹配服务：</strong>DataAnalysisService</p>
                    <p><strong>4. 调用方式：</strong>analytics_api.analyzeData()</p>
                `;
            } else {
                analysis = `
                    <h4>🔍 服务选择分析：</h4>
                    <p><strong>1. 意图识别：</strong>通用查询</p>
                    <p><strong>2. 关键词提取：</strong>未识别明确领域</p>
                    <p><strong>3. 匹配服务：</strong>GeneralSearchService</p>
                    <p><strong>4. 调用方式：</strong>search_api.generalSearch()</p>
                `;
            }
            
            result.innerHTML = analysis;
        }
        
        // 添加回车键支持
        document.getElementById('userInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                processInput();
            }
        });
    </script>
</body>
</html>